package com.scs.controller;

import com.alibaba.fastjson.JSONObject;
import com.scs.pojo.course;
import com.scs.pojo.relation;
import com.scs.service.courseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController{
    @Autowired
    private courseService courseService;


    @ResponseBody
    @RequestMapping(value="/getAllCourse", produces = "application/json;charset=utf-8")
    //管理员端查询课程
    public String getAllCourse(){
        JSONObject data = new JSONObject();
        List<course> courseList = courseService.getAllCourse();
        if (courseList.size()==0){
            data.put("code",110);
            data.put("data","");
            data.put("msg","获取课程列表失败");
            data.put("count",0);
        }else {
            data.put("code",0);
            data.put("data",courseList);
            data.put("msg","获取课程列表成功");
            data.put("count",courseList.size());
        }
        return data.toJSONString();
    }
    //修改课程
    @ResponseBody
    @RequestMapping(value = "/updateCourse",produces = "application/json;charset=utf-8")
    public String updateCourse(HttpServletRequest request){
        JSONObject data = new JSONObject();
        String courseId = request.getParameter("courseId");
        String courseName = request.getParameter("courseName");
        String courseScore = request.getParameter("courseScore");
        String courseTime = request.getParameter("courseTime");
        String courseMajor = request.getParameter("courseMajor");
        course course = new course(courseId,courseName,courseScore,courseTime,courseMajor);
        int count = courseService.updateCourse(course);
        if(count>0){
            data.put("success",1);
            data.put("msg","修改成功");
        }else{
            data.put("success",0);
            data.put("msg","修改失败");
        }
        data.put("count",count);
        data.put("data",null);
        return data.toJSONString();
    }
    //插入课程
    @ResponseBody
    @RequestMapping(value = "/insertCourse",produces = "application/json;charset=utf-8")
    public String insertCourse(HttpServletRequest request){
        JSONObject data = new JSONObject();
        String courseName = request.getParameter("courseName");
        String courseId = request.getParameter("courseId");
        String courseScore = request.getParameter("courseScore");
        String courseTime = request.getParameter("courseTime");
        String courseMajor = request.getParameter("courseMajor");
        course course = new course(courseId,courseName,courseScore,courseTime,courseMajor);
        int count = courseService.insertCourse(course);
        if(count>0){
            data.put("success",1);
            data.put("msg","添加成功");
        }else{
            data.put("success",0);
            data.put("msg","添加失败");
        }
        data.put("count",count);
        data.put("data",null);
        return data.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/deleteCourse",produces = "application/json;charset=utf-8")
    public String deleteCourse(HttpServletRequest request){
        JSONObject data = new JSONObject();
        String courseId = request.getParameter("courseId");
        int count = courseService.deleteCourse(courseId);
        if(count > 0){
            data.put("success",1);
            data.put("msg","删除成功");
        }else{
            data.put("success",0);
            data.put("msg","删除失败");
        }
        data.put("count",count);
        data.put("data",null);
        return data.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/batchRemoveCourse",produces = "application/json;charset=utf-8")
    public String batchRemoveCourse(HttpServletRequest request){
        JSONObject data = new JSONObject();
        List<String> List = JSONObject.parseArray(request.getParameter("courseIds"),String.class);
        int count = courseService.batchRemoveCourse(List);
        if(count > 0){
            data.put("success",1);
            data.put("msg","删除成功");
        }else{
            data.put("success",0);
            data.put("msg","删除失败");
        }
        data.put("count",count);
        data.put("data",null);
        return data.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/searchCourse",produces = "application/json;charset=utf-8")
    public String searchCourse(HttpServletRequest request){
        String key = request.getParameter("key");
        JSONObject data = new JSONObject();
        if (key == null || key.equals("")){
            List<course> allCourse = courseService.getAllCourse();
            data.put("code",0);
            data.put("msg","查询课程信息成功");
            data.put("data",allCourse);
            data.put("count",allCourse.size());
        }else {
            List<course> courseList = courseService.searchCourse(key);
            data.put("code",0);
            data.put("msg","查询课程信息成功");
            data.put("data",courseList);
            data.put("count",courseList.size());
        }
        return data.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/getCourseById",produces = "application/json;charset=utf-8")
    //根据Id查找课程
    public String getCourseById(HttpServletRequest request){
        String courseId = request.getParameter("courseId");
        JSONObject data = new JSONObject();
        if (courseId == null || courseId.equals("")){
            data.put("code", 0);
            data.put("msg", "课程ID参数无效");
            data.put("data","");
            data.put("count",0);
        }else {
            List<course> courseList = courseService.getCourseById(courseId);
            data.put("code", 0);
            data.put("msg", "获取课程分配列表成功");
            data.put("data",courseList);
            data.put("count",courseList.size());
        }


        return data.toJSONString();
    }
}
